package com.toommi.refuse.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.entity.PermissionGroup;
import com.toommi.refuse.entity.PermissionGroupExample;
import com.toommi.refuse.mapper.PermissionGroupMapper;
import com.toommi.refuse.service.admin.SysPermissionGroupService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yangg
 * @since 2019-07-30 11:54
 */
@Service
@AllArgsConstructor
public class SysPermissionGroupServiceImpl implements SysPermissionGroupService {

    private PermissionGroupMapper groupMapper;

    @Override
    public void addPermissionGroup(PermissionGroup permissionGroup) {
        groupMapper.insert(permissionGroup);
    }

    @Override
    public PermissionGroup findGroupByCode(String code) {

        PermissionGroup group = new PermissionGroup();
        group.setCode(code);

        return groupMapper.selectOne(group);

    }

    @Override
    public PageInfo<PermissionGroup> findPermissionGroups(int pageNum, int pageSize, String keyWord) {

        PageHelper.startPage(pageNum, pageSize);
        List<PermissionGroup> permissionGroupList;
        if (StringUtils.isEmpty(keyWord)) {
            permissionGroupList = groupMapper.selectAll();
        } else {
            PermissionGroupExample example = new PermissionGroupExample();
            example.createCriteria().andNameLike("%" + keyWord + "%");

            permissionGroupList = groupMapper.selectByExample(example);
        }

        return new PageInfo<>(permissionGroupList);
    }
}
